package model.market;

import java.util.Comparator;

public class TradeComparator implements Comparator<Trade>{
//AMLOW: this code is getting a touch complicated..
	private final int TRADE1_FIRST = -1;
	private final int TRADE2_FIRST = 1;
	
	public int compare(Trade trade1, Trade trade2) {
		if(trade1.getDirection()!=trade2.getDirection()){
			throw new RuntimeException("Trade compare fuckup");
		}
		
		if(trade1.isMarketOrder()
				&&(!trade2.isMarketOrder())){
			return TRADE1_FIRST;
		}else if(trade2.isMarketOrder()
				&&(!trade1.isMarketOrder())){
			return TRADE2_FIRST;
		}else {
			//Established that they are both aggressive..
			//Assume both are same direction..
			if(trade1.getDirection()==Trade.BUY){
				return trade1.getLimitPrice()>trade2.getLimitPrice()?
						TRADE1_FIRST:(trade1.getLimitPrice()<trade2.getLimitPrice()?TRADE2_FIRST:0);
			}else{//(trade1.getDirection()==Trade.SELL)
				return trade1.getLimitPrice()<trade2.getLimitPrice()?
						TRADE1_FIRST:(trade1.getLimitPrice()>trade2.getLimitPrice()?TRADE2_FIRST:0);
			}
			
		}
		
	}

	
}
